package com.dyn.videohub.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dyn.videohub.Common.R;
import com.dyn.videohub.Service.ReportService;
import com.dyn.videohub.domain.Report;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import static com.dyn.videohub.Common.CommonErrorEnums.*;

@Slf4j
@Controller
@RestController
@Api(tags = "举报管理",value = "举报接口")
@RequestMapping("/report")
public class ReportController {
    @Resource
    private ReportService reportService;

    /***
     * 提交举报
     * @param report
     * @return
     */
    @ApiOperation(value = "提交举报")
    @PostMapping("/submitReport")
    public R<String> submitReport(@RequestBody Report report) {
        LambdaQueryWrapper<Report> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Report::getUserId, report.getUserId()).eq(Report::getVideoId, report.getVideoId());
        Report r = reportService.getOne(lambdaQueryWrapper);
        if (r != null) {
            return R.error(REPORT_ERROR.getName());
        }
        return R.success(REPORT_SUCCESS.getName());
    }

    @GetMapping("/getReport")
    public List<Report> getReport(){
        return reportService.list();
    }

    //管理员处理举报
    @DeleteMapping("/reportDel")
    public R<String> reportDel(@RequestParam("reportId") int reportId){
        //先判断是否存在然后再执行操作
        Report r=reportService.getById(reportId);
        if(r==null){
            return R.error(DEAL_REPORT_ERROR.getName());
        }
        reportService.removeById(r);
        return R.success(DEAL_REPORT_SUCCESS.getName());
    }
}
